package com.wdc.controller;



import com.wdc.annotaion.GlobalInterceptor;
import com.wdc.entity.dto.TokenUserInfoDto;
import com.wdc.entity.po.GroupInfo;
import com.wdc.entity.po.UserContact;
import com.wdc.entity.query.GroupInfoQuery;
import com.wdc.entity.query.UserContactQuery;
import com.wdc.entity.vo.GroupInfoVo;
import com.wdc.entity.vo.ResponseVo;
import com.wdc.enums.GroupStatusEnum;
import com.wdc.enums.UserContactStatusEnum;
import com.wdc.exception.BusinessException;
import com.wdc.service.GroupInfoService;
import com.wdc.service.UserContactService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import java.io.IOException;
import java.util.List;

/**
 * @author: WDC
 * @description:Controller
 * @date:2024/09/19
 */
@RestController
@RequestMapping("/group")
public class GroupInfoController extends ABaseController {


	@Resource
	private GroupInfoService groupInfoService;

	@Resource
	private UserContactService userContactService;

	@RequestMapping("/saveGroup")
	@GlobalInterceptor
	public ResponseVo saveGroup(HttpServletRequest request, String groupId,
								String groupName,
								String groupNotice,
								Integer joinType,
								MultipartFile avatarFile,
								MultipartFile avatarCover) throws IOException {

		TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
		GroupInfo groupInfo = new GroupInfo();
		groupInfo.setGroupId(groupId);
		groupInfo.setGroupName(groupName);
		groupInfo.setGroupOwnerId(tokenUserInfo.getUserId());
		groupInfo.setGroupNotice(groupNotice);
		groupInfo.setJoinType(joinType);
		this.groupInfoService.saveGroup(groupInfo, avatarFile, avatarCover);
		return getSuccessResponseVo(null);

	}

	@RequestMapping("/loadMyGroup")
	@GlobalInterceptor
	public ResponseVo loadMyGroup(HttpServletRequest request) {
		TokenUserInfoDto tokenUserInfoDto = getTokenUserInfo(request);
		GroupInfoQuery groupInfoQuery = new GroupInfoQuery();
		groupInfoQuery.setGroupOwnerId(tokenUserInfoDto.getUserId());
		groupInfoQuery.setOrderBy("create_time desc");
		List<GroupInfo> groupInfoList = this.groupInfoService.findListByParam(groupInfoQuery);
		return getSuccessResponseVo(groupInfoList);
	}

	@RequestMapping("/getGroupInfo")
	@GlobalInterceptor
	public ResponseVo getGroupInfo(HttpServletRequest request, @NotEmpty String groupId) {
		GroupInfo groupInfo = getGroupDetailCommon(request, groupId);
		UserContactQuery userContactQuery = new UserContactQuery();
		userContactQuery.setContactId(groupId);
		Integer memberCount = this.userContactService.findCountByParam(userContactQuery);
		groupInfo.setMemberCount(memberCount);
		return getSuccessResponseVo(groupInfo);


	}

	private GroupInfo getGroupDetailCommon(HttpServletRequest request, String groupId) {
		TokenUserInfoDto tokenUserInfoDto = getTokenUserInfo(request);
		UserContact userContact = this.userContactService.getUserContactByUserIdAndContactId(tokenUserInfoDto.getUserId(), groupId);
		if (null == userContact || !UserContactStatusEnum.FRIEND.getStatus().equals(userContact.getStatus())) {
			throw new BusinessException("你不在群聊或者群聊不存在或已解散");
		}
		GroupInfo groupInfo = this.groupInfoService.getGroupInfoByGroupId(groupId);
		if (null == groupInfo || !GroupStatusEnum.NORMAL.getStatus().equals(groupInfo.getStatus())) {
			throw new BusinessException("群聊不存在");
		}

		return groupInfo;
	}

	@RequestMapping("/getGroupInfo4Chat")
	@GlobalInterceptor
	public ResponseVo getGroupInfo4Chat(HttpServletRequest request, @NotEmpty String groupId) {
		GroupInfo groupInfo = getGroupDetailCommon(request, groupId);
		UserContactQuery userContactQuery = new UserContactQuery();
		userContactQuery.setContactId(groupId);
		userContactQuery.setQueryUserInfo(true);
		userContactQuery.setOrderBy("create_time asc");
		userContactQuery.setStatus(UserContactStatusEnum.FRIEND.getStatus());
		List<UserContact> userContactList = this.userContactService.findListByParam(userContactQuery);
		GroupInfoVo groupInfoVo = new GroupInfoVo();
		groupInfoVo.setGroupInfo(groupInfo);
		groupInfoVo.setUserContactList(userContactList);
		return getSuccessResponseVo(groupInfoVo);
	}


}